﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;

namespace WebApplication1.session
{
    public partial class login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Cookies["accountCookie"] != null && Request.Cookies["pwdCookie"] != null)
            {
                string account = Request.Cookies["accountCookie"].Value;
                string pwd=Request.Cookies["pwdCookie"].Value;
                MySqlConnection cn = new MySqlConnection("server=localhost;uid = root;database=data;charset=utf8");
                //2）适配器帮我们查数据  参数：1）查什么 2）数据连接管道
                string sql = string.Format("select* from student where name='{0}' and pwd='{1}'", account, pwd);
                MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter(sql, cn);
                //3)创建一个内存容器，创建一个空容器
                DataSet ds = new DataSet();
                //4)将数据从适配器中填入到容器中
                mySqlDataAdapter.Fill(ds);

                if (ds.Tables[0].Rows.Count > 0)
                {
                    Session["account"] = ds.Tables[0].Rows[0]["name"].ToString();
                    Session["ImageUrl"] = ds.Tables[0].Rows[0]["img_url"].ToString();
                    Response.Redirect("Index.aspx");
                }
            }
            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string name=textname.Text.Trim();
            string pwd=textpwd.Text.Trim();
            MySqlConnection cn = new MySqlConnection("server=localhost;uid = root;database=data;charset=utf8");
            //2）适配器帮我们查数据  参数：1）查什么 2）数据连接管道
            string sql = string.Format("select* from student where name='{0}' and pwd='{1}'", name, pwd);
            MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter(sql,cn);
            //3)创建一个内存容器，创建一个空容器
            DataSet ds = new DataSet();
            //4)将数据从适配器中填入到容器中
            mySqlDataAdapter.Fill(ds);

            //判断用户是否要保存cookie
            if(ds.Tables[0].Rows.Count > 0)
            {
                if(CheckBox1.Checked==true)
                {
                    HttpCookie accountCookie = new HttpCookie("accountCookie");
                    accountCookie.Value = HttpUtility.UrlEncode(name);
                    accountCookie.Expires = DateTime.Now.AddMonths(1);

                    HttpCookie pwdCookie = new HttpCookie("pwdCookie");
                    pwdCookie.Value = HttpUtility.UrlEncode(pwd);
                    pwdCookie.Expires = DateTime.Now.AddMonths(1);
                    Response.Cookies.Add(accountCookie);
                    Response.Cookies.Add(pwdCookie);


                    
                }
                Session["User"] = ds.Tables[0].Rows[0]["name"].ToString();
                Session["ImageUrl"] = ds.Tables[0].Rows[0]["img_url"].ToString();
                Response.Redirect("Index.aspx");
            }
            else
            {
                Response.Write("<script>alert('账号或密码错误！');</script>");
            }

        }
    }
}